package strategyorder

import "fmt"

// DiscountStrategy 折扣策略接口
type DiscountStrategy interface {
	// 计算折扣
	CalculateDiscount(order *Order) float64
	// 获取描述
	GetDescription() string
}

// 具体策略实现

// NoDiscountStrategy 无折扣策略
type NoDiscountStrategy struct {
}

func (s *NoDiscountStrategy) CalculateDiscount(order *Order) float64 {
	return 0
}

func (s *NoDiscountStrategy) GetDescription() string {
	return "无折扣"
}

// FirstOrderDiscountStrategy 新用户首单折扣策略
type FirstOrderDiscountStrategy struct {
	// 首单折扣率
	discountRate float64
}

func NewFirstOrderDiscountStrategy(discountRate float64) *FirstOrderDiscountStrategy {
	return &FirstOrderDiscountStrategy{
		discountRate: discountRate,
	}
}

func (s *FirstOrderDiscountStrategy) CalculateDiscount(order *Order) float64 {
	if order.IsFirstOrder {
		return order.TotalAmount * s.discountRate
	}
	return 0
}

func (s *FirstOrderDiscountStrategy) GetDescription() string {
	return fmt.Sprintf("新用户首单 %.0f%% 折扣", s.discountRate*100)
}

// MemberDiscountStrategy 会员等级折扣策略
type MemberDiscountStrategy struct {
}

func NewMemberDiscountStrategy() *MemberDiscountStrategy {
	return &MemberDiscountStrategy{}
}

func (s *MemberDiscountStrategy) CalculateDiscount(order *Order) float64 {
	switch order.MemberLevel {
	case "gold":
		return order.TotalAmount * 0.1
	case "platinum":
		return order.TotalAmount * 0.15
	default:
		return 0
	}
}

func (s *MemberDiscountStrategy) GetDescription() string {
	return "会员等级折扣"
}

// CouponDiscountStrategy 优惠券折扣策略
type CouponDiscountStrategy struct {
	coupons map[string]float64 // 优惠券代码 -> 折扣率
}

func NewCouponDiscountStrategy() *CouponDiscountStrategy {
	return &CouponDiscountStrategy{
		coupons: map[string]float64{
			"SAVE10":   0.10, // 10% 折扣
			"SAVE20":   0.20, // 20% 折扣
			"FREESHIP": 0,    // 免运费（这里简化处理）
		},
	}
}

func (s *CouponDiscountStrategy) CalculateDiscount(order *Order) float64 {
	if discount, exists := s.coupons[order.CouponCode]; exists {
		return order.TotalAmount * discount
	}
	return 0
}

func (s *CouponDiscountStrategy) GetDescription() string {
	return "优惠券折扣"
}

// ThresholdDiscountStrategy 	满额折扣策略
type ThresholdDiscountStrategy struct {
	threshold float64 // 满额金额
	discount  float64 // 折扣金额
}

func NewThresholdDiscountStrategy(threshold, discount float64) *ThresholdDiscountStrategy {
	return &ThresholdDiscountStrategy{
		threshold: threshold,
		discount:  discount,
	}
}

func (s *ThresholdDiscountStrategy) CalculateDiscount(order *Order) float64 {
	if order.TotalAmount >= s.threshold {
		return s.discount
	}
	return 0
}

func (s *ThresholdDiscountStrategy) GetDiscription() string {
	return fmt.Sprintf("满 %.2f 减 %.2f", s.threshold, s.discount)
}

// CompositeDiscountStrategy 组合折扣策略
type CompositeDiscountStrategy struct {
	strategies []DiscountStrategy
}

func NewCompositeDiscountStrategy(strategies ...DiscountStrategy) *CompositeDiscountStrategy {
	return &CompositeDiscountStrategy{
		strategies: strategies,
	}
}

func (s *CompositeDiscountStrategy) CalculateDiscount(order *Order) float64 {
	totalDiscount := 0.0
	for _, strategy := range s.strategies {
		totalDiscount += strategy.CalculateDiscount(order)
	}
	return totalDiscount
}

func (s *CompositeDiscountStrategy) GetDescription() string {
	desc := "组合折扣: ["
	for i, strategy := range s.strategies {
		if i > 0 {
			desc += " + "
		}
		desc += strategy.GetDescription()
	}
	desc += "]"
	return desc
}